﻿#region zh-CHS 2006 - 2007 DemoSoft 团队 | en 2006 - 2007 DemoSoft Team

//     NOTES
// ---------------
//
// This file is a part of the G.O.S.E(Game Online Server Engine) for .NET.
//
//                 2006-2007 DemoSoft Team
//
//
// First Version : by H.Q.Cai - mailto:caihuanqing@hotmail.com

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published
 *   by the Free Software Foundation; either version 2.1 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

#region zh-CHS 包含名字空间 | en Include namespace
using System;
using System.Text;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using Demo_G.O.S.E.ServerEngine.Common;
#endregion

namespace Demo_G.O.S.E.ServerEngine.Network.DLL
{
    /// <summary>
    /// 客户端的主处理行为
    /// </summary>
    [MultiThreadedSupportgAttribute( "当前的类所有成员都可锁定,支持多线程操作" )]
    public class ACESocketHandlerManagerAtClient
    {
        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 主处理指针
        /// </summary>
        private IntPtr m_Value      = IntPtr.Zero;

        /// <summary>
        /// 为优化而采用内存池
        /// </summary>
        static internal ACEProcessMessageBlockPool<ACEMessageBlock> s_InternalMessageBlockPool = new ACEProcessMessageBlockPool<ACEMessageBlock>("ACESocketHandlerManagerAtClient - ACEMessageBlock", 1024);
        #endregion

        #region zh-CHS 共有方法 | en Public Methods
        /// <summary>
        /// 给出连接的服务端的处理句柄
        /// </summary>
        /// <returns></returns>
        public ACEClientHandlerAtClient GetClientHandler()
        {
            IntPtr m_newValue = IntPtr.Zero;

            if ( ACESocketHandlerManagerAtClient.ACESocketHandlerManagerAtClient_GetClientHandler( ref m_newValue, m_Value ) == false )
                return null;

            if ( m_newValue == IntPtr.Zero )
                return null;

            ACEClientHandlerAtClient pClientHandlerAtClient = new ACEClientHandlerAtClient();
            pClientHandlerAtClient.Value = m_newValue;

            return pClientHandlerAtClient;
        }

        /// <summary>
        /// 给出新的数据区
        /// </summary>
        /// <returns></returns>
        public ACEMessageBlock GetNewSendMessageBlock()
        {
            IntPtr m_newMessageBlockValue = IntPtr.Zero;
            if (ACESocketHandlerManagerAtClient.ACESocketHandlerManagerAtClient_GetNewSendMessageBlock(ref m_newMessageBlockValue, m_Value) == false)
                return null;

            if (m_newMessageBlockValue == IntPtr.Zero)
                return null;

            // 从内存池获取内存 - 调用太频繁内存池优化
            ACEMessageBlock newSendMessageBlock = ACESocketHandlerManagerAtClient.s_InternalMessageBlockPool.AcquirePoolContent();
            newSendMessageBlock.Value           = m_newMessageBlockValue;

            return newSendMessageBlock;
        }
        #endregion

        #region zh-CHS 内部属性 | en Internal Properties
        /// <summary>
        /// 主处理指针
        /// </summary>
        protected internal IntPtr Value
        {
            get { return m_Value; }
            set { m_Value = value; }
        }
        #endregion

        #region zh-CHS 引入DLL接口 | en DLL Import
        /// <summary>
        /// dll 调用
        /// </summary>
        /// <param name="pClientHandlerAtClient"></param>
        /// <param name="pSocketHandlerManagerAtClient"></param>
        /// <returns></returns>
        [DllImport("Demo G.O.S.E.Net.dll", CallingConvention = CallingConvention.StdCall)]
        private static extern bool ACESocketHandlerManagerAtClient_GetClientHandler( ref IntPtr pClientHandlerAtClient, IntPtr pSocketHandlerManagerAtClient );

        /// <summary>
        /// dll 调用
        /// </summary>
        /// <param name="pNewSendMessageBlock"></param>
        /// <param name="pSocketHandlerManagerAtClient"></param>
        /// <returns></returns>
        [DllImport("Demo G.O.S.E.Net.dll", CallingConvention = CallingConvention.StdCall)]
        private static extern bool ACESocketHandlerManagerAtClient_GetNewSendMessageBlock( ref IntPtr pNewSendMessageBlock, IntPtr pSocketHandlerManagerAtClient );
        #endregion
    }
}
#endregion

